<template>
  <div :id="id" :class="className" :style="{height:'210px',width:'100%'}" />
</template>

<script>
import * as echarts from "echarts";

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart1'
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      chart: null,
      series: [],
    }
  },
  watch: {
    list: {
      handler() {
        this.initChart()
      },
      deep: true
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      let areaStyles = [
        {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0,
            color: 'rgba(1, 191, 201, 0.3)'
          }, {
            offset: 0.8,
            color: 'rgba(1, 191, 201, 0)'
          }], false),
          shadowColor: 'rgba(0, 0, 0, 0.1)',
          shadowBlur: 10
        },
        {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0,
            color: 'rgba(252, 164, 49, 0.3)'
          }, {
            offset: 0.8,
            color: 'rgba(252, 164, 49, 0)'
          }], false),
          shadowColor: 'rgba(0, 0, 0, 0.1)',
          shadowBlur: 10
        }
      ]
      let colorMap = {'办公区': '#128DEB', '生活区': '#FCA431', '厨房区': '#91CC75', '月出': '#EE6666', '仓库区': '#01BFC9'}
      this.series = []
      this.list.forEach((i, idx) => {
        this.series.push({
          name: i.name,
          type: 'line',
          smooth: true,
          symbolSize: 4,
          showSymbol: true,
          lineStyle: {
            normal: {
              width: 2
            }
          },
          areaStyle: {
            normal: areaStyles[idx] ? areaStyles[idx] : {shadowBlur: 10}
          },
          itemStyle: {
            normal: colorMap[i.name] ? {color: colorMap[i.name], borderColor: '#fff'} : {borderColor: '#fff'}
          },
          data: i.time_data.map(j => j.total_use)
        })
      });

      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        title: {
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              color: '#57617B'
            }
          }
        },
        grid: {
          // left: '5%',
          // right: '5%',
          borderWidth: 0,
          top: 50,
          bottom: 20,
          textStyle: {
            color: '#fff'
          }
        },
        legend: {
          top: '10',
          textStyle: {
            color: '#fff'
          },
          itemHeight: 10,
          data: this.list.map(i => {return {icon: 'circle', name: i.name}})
        },
        xAxis: [{
          type: 'category',
          boundaryGap: false,
          axisLabel: {
            show: true,
            textStyle: {
                color: 'rgba(255, 255, 255, 0.8)', // 文字颜色
                fontSize: 12 // 文字的字体大小
            }
         },
         axisLine: {
            lineStyle: {
                color: 'rgba(255, 255, 255, 0.8)' // 设置轴线颜色为红色
            }
         },
         axisTick:{
          show: false,
         },
          data: this.list.length == 0 ? [] : this.list[0].time_data.map(i => i.time.substring(11, 16))
        }],
        yAxis: [{
          type: 'value',
          axisLabel: {
            show: true,
            textStyle: {
                color: 'rgba(255, 255, 255, 0.8)', // 文字颜色
                fontSize: 12 // 文字的字体大小
            }
         },
          splitLine:{
            lineStyle:{
              type:'dashed',//y轴分割线类型 dashed为虚线
              color:'#3F4E68'
            }
          },
        }],
        series: this.series
      })
    }
  }
}
</script>
